<template>
  <el-dialog
    draggable
    overflow
    :title="titleMap[mode]"
    v-model="visible"
    :width="800"
    destroy-on-close
    @closed="$emit('closed')"
  >
    <sk-tabs type="border-card">
      <el-tab-pane ref="tab" label="基础信息">
        <el-form :model="form" :rules="rules" ref="dialogForm" label-width="70px">
          <el-row>
            <el-col :span="6">
              <el-form-item label="搬运人" prop="company_id">
                <skAutoChoose
                  v-model="form.company.name"
                  :name="'factory'"
                  :asKey="'company_id'"
                  :assign="form"
                  :params="[{ category_id: 110 }]"
                >
                </skAutoChoose>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="金额" prop="amount">
                <el-input v-model="form.amount" clearable></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="编号" prop="code">
                <el-input
                  label="编号"
                  placeholder="自动生成"
                  v-model="form.code"
                  readonly
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="单据日期" prop="billdate">
                <el-date-picker
                  v-model="form.billdate"
                  type="date"
                  value-format="YYYY-MM-DD"
                  placeholder="请选择日期"
                >
                </el-date-picker>
              </el-form-item>
            </el-col>

            <el-col :span="24">
              <el-form-item label="备注" prop="remark">
                <el-input v-model="form.remark" clearable></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <sc-VxeTable
            ref="xGrid1"
            :data="form.details"
            :gridOps="gridOps"
            :tablecode="'comprehensivel_carry_save'"
          >
          </sc-VxeTable>
        </el-form>
      </el-tab-pane>
      <el-tab-pane label="附件">
        <sk-upload
          v-model="form.attachments"
          :limit="10"
          draggable
          tip="单个文件不要超过10M,请上传图像格式文件"
        >
        </sk-upload>
      </el-tab-pane>
    </sk-tabs>

    <template #footer>
      <el-button @click="visible = false">取 消</el-button>
      <el-button type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
    </template>
  </el-dialog>
</template>

<script>
import skDicSelect from "@/components/skDicSelect";
import skAutoChoose from "@/components/skAutoChoose";
import tool from "@/utils/tool";
export default {
  emits: ["success", "closed"],
  components: { skDicSelect, skAutoChoose },
  data() {
    return {
      mode: "edit",
      openChoose: {
        order: false,
        storage: false,
      },

      gridOps: {
        height: 300,
        columns: [
          {
            field: "material.name",
            event: {
              select: ({ e, row }) => {
                row["material"]["spec"] = e.spec;
              },
            },
          },
          {
            field: "color.name",
            event: {
              select: ({ e, row }) => {
                row["color"]["code"] = e.code;
              },
            },
          },
          {
            field: "auxiliary_quantity",
            event: {
              keyup: ({ row }) => {
                row["quantity"] = row["auxiliary_quantity"] * 25;
              },
            },
          },
        ],
      },
      titleMap: {
        add: "新增",
        edit: "编辑",
      },

      form: {
        id: "",
        code: "",
        company: {
          name: "",
        },
        department: {},
        details: [],
        attachments: [],
        billdate: tool.SYS_DATE(),
      },
      rules: {
        billdate: [{ required: true, message: "请输入型号规格" }],
        company_id: [{ required: true, message: "运输商必填" }],
        department_id: [{ required: true, message: "请填写部门" }],
      },
      visible: false,
      isSaveing: false,
    };
  },
  mounted() {},
  methods: {
    //显示
    open(mode = "add") {
      this.mode = mode;
      this.visible = true;

      return this;
    },

    //表单提交方法
    submit() {
      this.$refs.dialogForm.validate(async (valid) => {
        if (valid) {
          this.isSaveing = true;
          var res = await this.$API.comprehensive.carry.save.post(this.form);
          this.isSaveing = false;
          if (res.code == 200) {
            this.$emit("success", this.form, this.mode);
            this.visible = false;
            this.$message.success("操作成功");
          }
        }
      });
    },
    async getItems(id) {
      var res = await this.$API.comprehensive.carry.details.get({
        filter: [{ main_id: id }],
      });
      if (res.code == 200) {
        this.form.details = res.data;
      } else {
        this.$alert(res.message, "提示", { type: "error" });
      }
    },

    //表单注入数据
    setData(data) {
      Object.assign(this.form, tool.removeNull(data));
      this.getItems(data.id);
    },
  },
};
</script>

<style></style>
